{% extends 'base.html' %}
{% load static %}

{% block title %}首页 - 固定资产管理系统{% endblock %}

{% block extra_css %}
<style>
    :root {
        --primary-color: #2563eb;
        --secondary-color: #64748b;
        --success-color: #10b981;
        --warning-color: #f59e0b;
        --danger-color: #ef4444;
        --info-color: #06b6d4;
        
        --gradient-primary: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        --gradient-success: linear-gradient(135deg, #10b981 0%, #059669 100%);
        --gradient-warning: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
        --gradient-danger: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
        --gradient-info: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
        
        --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
        --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
        --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
        --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
        
        --border-radius: 12px;
        --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    body {
        background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
        font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
        color: #1e293b;
    }

    /* 顶部导航栏样式 */
    .top-navbar {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(20px);
        border-bottom: 1px solid rgba(0, 0, 0, 0.05);
        padding: 1rem 0;
        position: sticky;
        top: 0;
        z-index: 1000;
    }

    .navbar-brand {
        font-size: 1.5rem;
        font-weight: 700;
        color: var(--primary-color);
        text-decoration: none;
    }

    .user-menu {
        display: flex;
        align-items: center;
        gap: 1rem;
    }

    .user-avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background: var(--gradient-primary);
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-weight: 600;
    }

    /* 欢迎区域 */
    .welcome-section {
        background: white;
        border-radius: var(--border-radius);
        padding: 2rem;
        margin-bottom: 2rem;
        box-shadow: var(--shadow-md);
        position: relative;
        overflow: hidden;
    }

    .welcome-section::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 4px;
        background: var(--gradient-primary);
    }

    .welcome-title {
        font-size: 2rem;
        font-weight: 700;
        color: #1e293b;
        margin-bottom: 0.5rem;
    }

    .welcome-subtitle {
        color: var(--secondary-color);
        font-size: 1.1rem;
        margin-bottom: 1.5rem;
    }

    .quick-actions {
        display: flex;
        gap: 1rem;
        flex-wrap: wrap;
    }

    .quick-action-btn {
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.75rem 1.5rem;
        background: var(--gradient-primary);
        color: white;
        text-decoration: none;
        border-radius: var(--border-radius);
        font-weight: 500;
        transition: var(--transition);
        box-shadow: var(--shadow-md);
    }

    .quick-action-btn:hover {
        transform: translateY(-2px);
        box-shadow: var(--shadow-lg);
        color: white;
    }

    .quick-action-btn.secondary {
        background: white;
        color: var(--primary-color);
        border: 2px solid var(--primary-color);
    }

    .quick-action-btn.secondary:hover {
        background: var(--primary-color);
        color: white;
    }

    /* 统计卡片网格 */
    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        gap: 1.5rem;
        margin-bottom: 2rem;
    }

    .stat-card {
        background: white;
        border-radius: var(--border-radius);
        padding: 1.5rem;
        box-shadow: var(--shadow-md);
        transition: var(--transition);
        position: relative;
        overflow: hidden;
    }

    .stat-card:hover {
        transform: translateY(-4px);
        box-shadow: var(--shadow-xl);
    }

    .stat-card::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 4px;
        background: var(--card-gradient);
    }

    .stat-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 1rem;
    }

    .stat-icon {
        width: 48px;
        height: 48px;
        border-radius: var(--border-radius);
        background: var(--card-gradient);
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 1.25rem;
    }

    .stat-number {
        font-size: 2.5rem;
        font-weight: 800;
        color: #1e293b;
        line-height: 1;
        margin-bottom: 0.5rem;
    }

    .stat-label {
        color: var(--secondary-color);
        font-size: 0.875rem;
        font-weight: 500;
        margin-bottom: 0.75rem;
    }

    .stat-trend {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.875rem;
        font-weight: 500;
    }

    .trend-up {
        color: var(--success-color);
    }

    .trend-down {
        color: var(--danger-color);
    }

    .trend-neutral {
        color: var(--secondary-color);
    }

    /* 主要内容区域 */
    .main-content {
        display: grid;
        grid-template-columns: 2fr 1fr;
        gap: 2rem;
        margin-bottom: 2rem;
    }

    /* 功能模块网格 */
    .modules-section {
        background: white;
        border-radius: var(--border-radius);
        padding: 1.5rem;
        box-shadow: var(--shadow-md);
    }

    .section-title {
        font-size: 1.25rem;
        font-weight: 700;
        color: #1e293b;
        margin-bottom: 1.5rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .modules-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 1rem;
    }

    .module-card {
        padding: 1.5rem;
        border: 2px solid #f1f5f9;
        border-radius: var(--border-radius);
        text-decoration: none;
        color: inherit;
        transition: var(--transition);
        cursor: pointer;
    }

    .module-card:hover {
        border-color: var(--primary-color);
        transform: translateY(-2px);
        box-shadow: var(--shadow-lg);
        color: inherit;
    }

    .module-icon {
        width: 40px;
        height: 40px;
        border-radius: 8px;
        background: var(--module-gradient);
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 1.125rem;
        margin-bottom: 1rem;
    }

    .module-title {
        font-size: 1rem;
        font-weight: 600;
        color: #1e293b;
        margin-bottom: 0.5rem;
    }

    .module-description {
        font-size: 0.875rem;
        color: var(--secondary-color);
        line-height: 1.4;
    }

    /* 侧边栏 */
    .sidebar {
        display: flex;
        flex-direction: column;
        gap: 1.5rem;
    }

    .sidebar-card {
        background: white;
        border-radius: var(--border-radius);
        padding: 1.5rem;
        box-shadow: var(--shadow-md);
    }

    .sidebar-title {
        font-size: 1.125rem;
        font-weight: 600;
        color: #1e293b;
        margin-bottom: 1rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    /* 活动列表 */
    .activity-list {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .activity-item {
        display: flex;
        align-items: flex-start;
        gap: 0.75rem;
        padding: 0.75rem;
        border-radius: 8px;
        transition: var(--transition);
    }

    .activity-item:hover {
        background: #f8fafc;
    }

    .activity-icon {
        width: 32px;
        height: 32px;
        border-radius: 6px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 0.875rem;
        flex-shrink: 0;
    }

    .activity-content {
        flex: 1;
        min-width: 0;
    }

    .activity-title {
        font-size: 0.875rem;
        font-weight: 500;
        color: #1e293b;
        margin-bottom: 0.25rem;
    }

    .activity-meta {
        font-size: 0.75rem;
        color: var(--secondary-color);
    }

    /* 状态指示器 */
    .status-list {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .status-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.75rem;
        border-radius: 8px;
        transition: var(--transition);
    }

    .status-item:hover {
        background: #f8fafc;
    }

    .status-label {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.875rem;
        font-weight: 500;
        color: #1e293b;
    }

    .status-dot {
        width: 8px;
        height: 8px;
        border-radius: 50%;
    }

    .status-value {
        font-size: 0.875rem;
        font-weight: 600;
        padding: 0.25rem 0.75rem;
        border-radius: 20px;
        color: white;
    }

    /* 响应式设计 */
    @media (max-width: 1024px) {
        .main-content {
            grid-template-columns: 1fr;
        }
        
        .modules-grid {
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
        }
    }

    @media (max-width: 768px) {
        .stats-grid {
            grid-template-columns: repeat(2, 1fr);
        }
        
        .quick-actions {
            flex-direction: column;
        }
        
        .quick-action-btn {
            justify-content: center;
        }
        
        .modules-grid {
            grid-template-columns: 1fr;
        }
    }

    @media (max-width: 480px) {
        .stats-grid {
            grid-template-columns: 1fr;
        }
        
        .welcome-section {
            padding: 1.5rem;
        }
        
        .welcome-title {
            font-size: 1.5rem;
        }
    }

    /* 动画效果 */
    @keyframes fadeInUp {
        from {
            opacity: 0;
            transform: translateY(20px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .animate-fade-in-up {
        animation: fadeInUp 0.6s ease-out forwards;
    }

    .animate-delay-1 { animation-delay: 0.1s; }
    .animate-delay-2 { animation-delay: 0.2s; }
    .animate-delay-3 { animation-delay: 0.3s; }
    .animate-delay-4 { animation-delay: 0.4s; }
</style>
{% endblock %}

{% block content %}
<!-- 顶部导航栏 -->
<div class="top-navbar">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <a href="{% url 'assets:index' %}" class="navbar-brand">
                <i class="fas fa-building me-2"></i>固定资产管理系统
            </a>
            <div class="user-menu">
                <span class="text-muted">欢迎回来，</span>
                <div class="user-avatar">
                    {{ user.first_name|first|default:user.username|first|upper }}
                </div>
                <span class="fw-medium">{{ user.get_full_name|default:user.username }}</span>
            </div>
        </div>
    </div>
</div>

<div class="container mt-4">
    <!-- 欢迎区域 -->
    <div class="welcome-section animate-fade-in-up">
        <div class="row align-items-center">
            <div class="col-md-8">
                <h1 class="welcome-title">
                    <i class="fas fa-sun text-warning me-2"></i>
                    {% now "H" as current_hour %}
                    {% if current_hour < "12" %}早上好{% elif current_hour < "18" %}下午好{% else %}晚上好{% endif %}，{{ user.get_full_name|default:user.username }}！
                </h1>
                <p class="welcome-subtitle">
                    今天是 {% now "Y年m月d日 l" %}，让我们开始高效的资产管理工作吧
                </p>
                <div class="quick-actions">
                    <a href="{% url 'assets:asset_create' %}" class="quick-action-btn">
                        <i class="fas fa-plus"></i>
                        新增资产
                    </a>
                    <a href="{% url 'assets:asset_list' %}" class="quick-action-btn secondary">
                        <i class="fas fa-list"></i>
                        资产列表
                    </a>
                    <a href="{% url 'assets:asset_report' %}" class="quick-action-btn secondary">
                        <i class="fas fa-chart-bar"></i>
                        查看报表
                    </a>
                </div>
            </div>
            <div class="col-md-4 text-center">
                <div class="position-relative">
                    <i class="fas fa-chart-line fa-5x text-primary opacity-25"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-grid">
        <div class="stat-card animate-fade-in-up animate-delay-1" style="--card-gradient: var(--gradient-primary);">
            <div class="stat-header">
                <div class="stat-icon">
                    <i class="fas fa-boxes"></i>
                </div>
            </div>
            <div class="stat-number">{{ total_assets|default:0 }}</div>
            <div class="stat-label">资产总数</div>
            <div class="stat-trend trend-up">
                <i class="fas fa-arrow-up"></i>
                本月新增 {{ monthly_new_assets|default:0 }} 个
            </div>
        </div>

        <div class="stat-card animate-fade-in-up animate-delay-2" style="--card-gradient: var(--gradient-success);">
            <div class="stat-header">
                <div class="stat-icon">
                    <i class="fas fa-play-circle"></i>
                </div>
            </div>
            <div class="stat-number">{{ in_use_count|default:0 }}</div>
            <div class="stat-label">使用中资产</div>
            <div class="stat-trend trend-up">
                <i class="fas fa-percentage"></i>
                使用率 {% if total_assets > 0 %}{% widthratio in_use_count total_assets 100 %}{% else %}0{% endif %}%
            </div>
        </div>

        <div class="stat-card animate-fade-in-up animate-delay-3" style="--card-gradient: var(--gradient-info);">
            <div class="stat-header">
                <div class="stat-icon">
                    <i class="fas fa-users"></i>
                </div>
            </div>
            <div class="stat-number">{{ active_users|default:0 }}</div>
            <div class="stat-label">活跃用户</div>
            <div class="stat-trend trend-neutral">
                <i class="fas fa-building"></i>
                {{ departments_count|default:0 }} 个部门
            </div>
        </div>

        <div class="stat-card animate-fade-in-up animate-delay-4 {% if low_stock_consumables > 0 %}border-warning{% endif %}" 
             style="--card-gradient: {% if low_stock_consumables > 0 %}var(--gradient-warning){% else %}var(--gradient-success){% endif %};">
            <div class="stat-header">
                <div class="stat-icon">
                    <i class="fas fa-{% if low_stock_consumables > 0 %}exclamation-triangle{% else %}check-circle{% endif %}"></i>
                </div>
            </div>
            <div class="stat-number">{{ low_stock_consumables|default:0 }}</div>
            <div class="stat-label">库存预警</div>
            <div class="stat-trend {% if low_stock_consumables > 0 %}trend-down{% else %}trend-up{% endif %}">
                <i class="fas fa-{% if low_stock_consumables > 0 %}exclamation-circle{% else %}check-circle{% endif %}"></i>
                {% if low_stock_consumables > 0 %}需要关注{% else %}状态正常{% endif %}
            </div>
        </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
        <!-- 功能模块 -->
        <div class="modules-section animate-fade-in-up animate-delay-1">
            <h2 class="section-title">
                <i class="fas fa-th-large text-primary"></i>
                功能模块
            </h2>
            <div class="modules-grid">
                <a href="{% url 'assets:asset_list' %}" class="module-card" style="--module-gradient: var(--gradient-primary);">
                    <div class="module-icon">
                        <i class="fas fa-boxes"></i>
                    </div>
                    <div class="module-title">资产管理</div>
                    <div class="module-description">管理固定资产的全生命周期</div>
                </a>

                <a href="{% url 'assets:warehouse_list' %}" class="module-card" style="--module-gradient: var(--gradient-success);">
                    <div class="module-icon">
                        <i class="fas fa-warehouse"></i>
                    </div>
                    <div class="module-title">库存管理</div>
                    <div class="module-description">智能管理仓库和耗材库存</div>
                </a>

                <a href="{% url 'assets:inventory_plan_list' %}" class="module-card" style="--module-gradient: var(--gradient-info);">
                    <div class="module-icon">
                        <i class="fas fa-clipboard-check"></i>
                    </div>
                    <div class="module-title">资产盘点</div>
                    <div class="module-description">制定和执行盘点计划</div>
                </a>

                <a href="{% url 'assets:borrow_request_list' %}" class="module-card" style="--module-gradient: var(--gradient-warning);">
                    <div class="module-icon">
                        <i class="fas fa-handshake"></i>
                    </div>
                    <div class="module-title">借用归还</div>
                    <div class="module-description">管理资产借用和归还流程</div>
                </a>

                <a href="{% url 'assets:maintenance_list' %}" class="module-card" style="--module-gradient: var(--gradient-danger);">
                    <div class="module-icon">
                        <i class="fas fa-tools"></i>
                    </div>
                    <div class="module-title">维修管理</div>
                    <div class="module-description">跟踪资产维修和保养</div>
                </a>

                <a href="{% url 'assets:asset_report' %}" class="module-card" style="--module-gradient: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);">
                    <div class="module-icon">
                        <i class="fas fa-chart-bar"></i>
                    </div>
                    <div class="module-title">报表分析</div>
                    <div class="module-description">生成各类统计报表</div>
                </a>
            </div>
        </div>

        <!-- 侧边栏 -->
        <div class="sidebar">
            <!-- 系统状态 -->
            <div class="sidebar-card animate-fade-in-up animate-delay-2">
                <h3 class="sidebar-title">
                    <i class="fas fa-tachometer-alt text-primary"></i>
                    系统状态
                </h3>
                <div class="status-list">
                    <div class="status-item">
                        <div class="status-label">
                            <div class="status-dot bg-success"></div>
                            在用资产
                        </div>
                        <div class="status-value bg-success">{{ in_use_count|default:0 }}</div>
                    </div>
                    <div class="status-item">
                        <div class="status-label">
                            <div class="status-dot bg-secondary"></div>
                            闲置资产
                        </div>
                        <div class="status-value bg-secondary">{{ idle_count|default:0 }}</div>
                    </div>
                    <div class="status-item">
                        <div class="status-label">
                            <div class="status-dot bg-warning"></div>
                            维修中
                        </div>
                        <div class="status-value bg-warning">{{ maintenance_count|default:0 }}</div>
                    </div>
                    {% if low_stock_consumables > 0 %}
                    <div class="status-item">
                        <div class="status-label">
                            <div class="status-dot bg-danger"></div>
                            库存预警
                        </div>
                        <div class="status-value bg-danger">{{ low_stock_consumables }}</div>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- 最近活动 -->
            <div class="sidebar-card animate-fade-in-up animate-delay-3">
                <h3 class="sidebar-title">
                    <i class="fas fa-clock text-info"></i>
                    最近活动
                </h3>
                <div class="activity-list">
                    {% if recent_assets %}
                        {% for asset in recent_assets|slice:":5" %}
                        <div class="activity-item">
                            <div class="activity-icon bg-primary">
                                <i class="fas fa-plus"></i>
                            </div>
                            <div class="activity-content">
                                <div class="activity-title">新增资产：{{ asset.name }}</div>
                                <div class="activity-meta">
                                    {{ asset.created_at|timesince }}前
                                    {% if asset.department %} · {{ asset.department.name }}{% endif %}
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    {% endif %}
                    
                    {% if recent_maintenance %}
                        {% for maintenance in recent_maintenance|slice:":3" %}
                        <div class="activity-item">
                            <div class="activity-icon bg-warning">
                                <i class="fas fa-tools"></i>
                            </div>
                            <div class="activity-content">
                                <div class="activity-title">维修记录：{{ maintenance.asset.name }}</div>
                                <div class="activity-meta">
                                    {{ maintenance.created_at|timesince }}前
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    {% endif %}
                    
                    {% if not recent_assets and not recent_maintenance %}
                    <div class="text-center py-4">
                        <i class="fas fa-inbox fa-2x text-muted mb-2"></i>
                        <p class="text-muted mb-0">暂无最近活动</p>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- 快速链接 -->
            <div class="sidebar-card animate-fade-in-up animate-delay-4">
                <h3 class="sidebar-title">
                    <i class="fas fa-link text-success"></i>
                    快速链接
                </h3>
                <div class="d-grid gap-2">
                    <a href="{% url 'assets:asset_create' %}" class="btn btn-outline-primary btn-sm">
                        <i class="fas fa-plus me-1"></i>新增资产
                    </a>
                    <a href="{% url 'assets:bulk_import_assets' %}" class="btn btn-outline-secondary btn-sm">
                        <i class="fas fa-upload me-1"></i>批量导入
                    </a>
                    <a href="{% url 'assets:export_asset_report_excel' %}" class="btn btn-outline-success btn-sm">
                        <i class="fas fa-download me-1"></i>导出报表
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 添加页面加载动画
    const animatedElements = document.querySelectorAll('.animate-fade-in-up');
    animatedElements.forEach((element, index) => {
        element.style.opacity = '0';
        element.style.transform = 'translateY(20px)';
        
        setTimeout(() => {
            element.style.transition = 'all 0.6s ease-out';
            element.style.opacity = '1';
            element.style.transform = 'translateY(0)';
        }, index * 100);
    });

    // 统计数字动画
    const statNumbers = document.querySelectorAll('.stat-number');
    statNumbers.forEach(element => {
        const finalValue = parseInt(element.textContent);
        if (finalValue > 0) {
            animateNumber(element, 0, finalValue, 1000);
        }
    });

    // 卡片悬停效果
    const cards = document.querySelectorAll('.stat-card, .module-card, .sidebar-card');
    cards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-4px)';
        });
        
        card.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
        });
    });
});

// 数字动画函数
function animateNumber(element, start, end, duration) {
    const startTime = performance.now();
    
    function updateNumber(currentTime) {
        const elapsed = currentTime - startTime;
        const progress = Math.min(elapsed / duration, 1);
        
        const current = Math.floor(start + (end - start) * easeOutCubic(progress));
        element.textContent = current;
        
        if (progress < 1) {
            requestAnimationFrame(updateNumber);
        }
    }
    
    requestAnimationFrame(updateNumber);
}

// 缓动函数
function easeOutCubic(t) {
    return 1 - Math.pow(1 - t, 3);
}

// 实时时间更新
function updateTime() {
    const now = new Date();
    const timeString = now.toLocaleTimeString('zh-CN');
    const timeElements = document.querySelectorAll('.current-time');
    timeElements.forEach(element => {
        element.textContent = timeString;
    });
}

// 每秒更新时间
setInterval(updateTime, 1000);
updateTime();
</script>
{% endblock %}""